package com.shms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shms.bean.GroupOfHome;
import com.shms.dao.ShHomeDao;
import com.shms.entity.ShHome;
import com.shms.entity.ShPoint;
import com.shms.until.PageUntil;


/**
 * 文件名：ShHomeDaoImpl.java
 * 编写者：
 * 创建时间：2013-1-3
 * 简要描述：
 *
 */
public class ShHomeDaoImpl extends HibernateDaoSupport implements ShHomeDao {
	@SuppressWarnings({ "unchecked", "finally" })
	public List<ShHome> queryHomeByBid(long id) {
		List<ShHome> homeList = new ArrayList<ShHome>();
		try {
			String sql = "SELECT * FROM SH_HOME H WHERE H.BID = :id";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setParameter("id", id);
			homeList = sqlQuery.addEntity(ShHome.class).list();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return homeList;
		}
	}
	
	@SuppressWarnings("finally")
	@Override
	public GroupOfHome queryHome(GroupOfHome groupOfHome) {
		GroupOfHome gOfHomeRtn = new GroupOfHome();
		try {
			//查询数据总条数的SQL语句
			String sql = "SELECT COUNT(*) FROM SH_HOME";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//获取总条数
			int allRow = Integer.parseInt(String.valueOf(sqlQuery.uniqueResult()));
			//获取总页数
			int totalPage = PageUntil.countTotalPage(groupOfHome.getPageSize(), allRow);
			//获取要查询显示的数据的页数
			int currentPage = PageUntil.countCurrentPage(groupOfHome.getCurrentPage(), totalPage);
			//获取要查询显示的数据的第一条的数字
			int currentRow = PageUntil.countCurrentRow(groupOfHome.getPageSize(), currentPage);
			
			sql = "SELECT H.HID AS hid , B.BNAME AS park1 ,H.HNO AS hno FROM SH_BUILD B, SH_HOME H WHERE B.BID=H.BID";
			sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.addScalar("hid",Hibernate.INTEGER).addScalar("park1", Hibernate.STRING).addScalar("hno", Hibernate.STRING);
			//查询结果转换为对象
			sqlQuery.setResultTransformer(Transformers.aliasToBean(ShHome.class));
			//设置查询的第一条数据位置
			sqlQuery.setFirstResult(currentRow);
			//设置查询最大条数
			sqlQuery.setMaxResults(groupOfHome.getPageSize());
			@SuppressWarnings("unchecked")
			List<ShHome> homesListRtn = sqlQuery.list();
			gOfHomeRtn = new GroupOfHome(currentPage, totalPage, allRow, homesListRtn); 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return gOfHomeRtn;
		}
	}

	@SuppressWarnings("finally")	
	@Override
	public boolean batchDelete(Integer[] ids) {
		boolean rtn = true;
		try {
			String sql = "DELETE FROM SH_HOME H  WHERE H.HID NOT IN(SELECT H.HID FROM SH_HOME H,SH_POINT P WHERE H.HID=P.HID ) AND H.HID IN (:ids)" ;
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setParameterList("ids", ids);
			sqlQuery.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean saveHome(ShHome shHome) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().save(shHome);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean updateHome(ShHome shHome) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().update(shHome);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public ShHome queryById(ShHome shHome) {
		ShHome shHomeRtn = new ShHome();
		try {
			String sql = "SELECT * FROM SH_HOME WHERE HID = :hid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shHome);
			@SuppressWarnings("unchecked")
			List<ShHome> listHome = sqlQuery.addEntity(ShHome.class).list();
			if (listHome.size() == 1) {
				shHomeRtn = listHome.get(0);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shHomeRtn;
		}
	}

	@SuppressWarnings({ "unchecked", "finally" })
	@Override
	public List<ShPoint> queryPointsByHId(ShHome shHome) {
		List<ShPoint> shc = new ArrayList<ShPoint>();
		try {
			String sql = "SELECT P.* FROM SH_POINT P,SH_HOME H WHERE P.HID= H.HID AND P.HID= :hid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shHome);
			shc = sqlQuery.addEntity(ShPoint.class).list();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shc;
		}
	}
}
